package game.objects.nonstaticObjects;

import engine.graphics.IRender;
import engine.graphics.Renderer;
import engine.graphics.Vertex2f;
import engine.objects.Line;

public class Rope extends Line implements IRender{

	private static final long serialVersionUID = 1L;

	private EntityModel figure ; 
	private boolean hanging = false ; 
	private float shootingSpeed = 15.0f ; 
	private Vertex2f shootingVelocity  ; 
	
	public Rope(EntityModel fig, float x1, float y1, float x2, float y2) {
		super(x1, y1, x2, y2);
		this.figure = fig ; 
		int x = 1, y = 1 ; 
		if(x2<x1) x = -1 ; 
		if(y2<y1) y = -1 ; 
		shootingVelocity = new Vertex2f(shootingSpeed*x, shootingSpeed*y*getGradient());
		setX2(x1+shootingVelocity.getX());
		setY2(y1+shootingVelocity.getY()); 
	}

	public void update(){
		setX1((float)figure.getCenterX()); 
		setY1((float)figure.getCenterY()); 
		shoot(); 
	}
	
	public void shoot() { 
		if(!hanging){
			if(!figure.getManager().getGameObjects().collidingExcept(this, figure, shootingVelocity).isColliding()){
				setX2(getX2()+shootingVelocity.getX()); 
				setY2(getY2()+shootingVelocity.getY());
				hanging = false ; 
			}else
				hanging = true ; 
		}
	}

	public boolean isHanging() {
		return hanging ;
	}
	
	public Vertex2f getShootingVelocity(){
		return this.shootingVelocity; 
	}

	@Override
	public void render() {
		Renderer.renderLine(this); 
	}

}
